﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using QSDesigner.Windows;

namespace QSDesigner
{
    public static class WindowManager
    {

        public static List<IAvgQueueLengthByChannelsWindow> GraphicsWindows = new List<IAvgQueueLengthByChannelsWindow>();

        /*private static event Action CloseRegionWindows = delegate { };

        public static void LaunchWindowNewThread<T>(List<DesignerItemViewModel> items) where T : Window, IAvgQueueLengthByChannelsWindow, new()
        {
            var newWindowThread = new Thread(ThreadStartingPoint);
            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            Func<Window> constructor = () => new T { Items = items };
            newWindowThread.Start(constructor);
        }

        public static void StopFetchDataSignal()
        {
            foreach (var graphicsWindow in GraphicsWindows)
            {
                graphicsWindow.StopFetchData();
            }
        }

        public static void StopFetchDataAndCloseWindows()
        {
            foreach (var graphicsWindow in GraphicsWindows)
            {
                graphicsWindow.StopFetchData();
            }
            CloseRegionWindows();
        }

        public static void StartFetchDataSignalWithUpdate(List<DesignerItemViewModel> items)
        {
            foreach (var graphicsWindow in GraphicsWindows)
            {
                UpdateModel(graphicsWindow, items);
                graphicsWindow.StartFetchData();
            }
        }

        public static void StartFetchDataSignal()
        {
            foreach (var graphicsWindow in GraphicsWindows)
            {
                graphicsWindow.StartFetchData();
            }
        }

        private static void UpdateModel(IAvgQueueLengthByChannelsWindow avgQueueLengthByChannelsWindow, List<DesignerItemViewModel> items)
        {
            avgQueueLengthByChannelsWindow.UpdateModel(items);
        }

        private static void ThreadStartingPoint(object t)
        {
            // Create our context, and install it:
            SynchronizationContext.SetSynchronizationContext(
                new DispatcherSynchronizationContext(
                    Dispatcher.CurrentDispatcher));

            var constructor = (Func<Window>)t;
            var win = constructor();

            GraphicsWindows.Add((IAvgQueueLengthByChannelsWindow)win);

            CloseRegionWindows += () => win.Dispatcher.BeginInvoke(new ThreadStart(win.Close));

            // When the window closes, shut down the dispatcher
            win.Closed += (s, e) =>
                              {
                                  GraphicsWindows.Remove((IAvgQueueLengthByChannelsWindow)s);
                                  Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
                              };

            win.Show();

            ((IAvgQueueLengthByChannelsWindow)win).StartFetchData();

            // Start the Dispatcher Processing
            Dispatcher.Run();
        }*/

        // было когда то.. когда передавали блок в окно
        /*public static void LaunchWindowNewThread<T>(DesignerItem designerItem) where T : Window, IDesignerWindow,  new()
        {
            var newWindowThread = new Thread(ThreadStartingPoint);
            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            Func<Window> constructor = () => new T {Item = designerItem};
            newWindowThread.Start(constructor);
        }

        private static void ThreadStartingPoint(object t)
        {
            // Create our context, and install it:
            SynchronizationContext.SetSynchronizationContext(
                new DispatcherSynchronizationContext(
                    Dispatcher.CurrentDispatcher));

            var constructor = (Func<Window>)t;
            var win = constructor();    
            
            // When the window closes, shut down the dispatcher
            win.Closed += (s, e) =>
               Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);

            win.Show();

            // Start the Dispatcher Processing
            Dispatcher.Run();
        }*/
    }
}
